<?php
// 设置防止用户未登录就进行使用
session_start();
if (!isset($_SESSION["role"])||($_SESSION["role"]!="student"))
    header("location:../index.php");
?>
<?php
        include("../functions/db_conn.php");

$stuno=$_SESSION["username"];
$cno=$_GET["socino"];

// 判断学生是否选社团
$ShowSoci_sql = "SELECT * FROM StuSoci WHERE StuNo ='$stuno' AND SociNo='$cno'";
$ShowSociResult = mysql_query($ShowSoci_sql);
$Sum = mysql_num_rows($ShowSociResult);
if ($Sum>0) {
    echo "<script>alert('该社团已经选！');javascript:window.history.go(-1);</script>";
}


// 判断社团是否被选满，选满则不能选择
$ShowSoci_num_sql = "SELECT * FROM StuSoci WHERE SociNo='$cno'";
$ShowSociResult = mysql_query($ShowSoci_num_sql);
$num = mysql_num_rows($ShowSociResult);

$ShowSoci_max_num_sql = "select LimitNum from Soci where SociNo='$cno'";
$ShowSociResult = mysql_query($ShowSoci_max_num_sql);
$max_num = mysql_fetch_array($ShowSociResult);

if ($max_num['LimitNum'] <= $num) {
    echo "<script>alert('该社团人数已满，请选择其他社团！');javascript:window.history.go(-1);</script>";
}
// 未选满，可以选择该社团
else{
    $maxorder=$_GET["maxorder"];
        $maxorder=$maxorder+1;
        $sql="INSERT INTO StuSoci VALUES(\"$stuno\",\"$cno\",$maxorder,'审核');";
        // echo $sql;
        $result=mysql_query($sql);
        // echo "success";
        if($result){
            echo "<script>javascript:window.history.go(-1);</script>";
        }
        else
        {
            echo "<script>javascript:window.history.go(-1);</script>";
        }

}
?>

